#include "mytestmodel.h"
#include <QVariant>
#include <QColor>
#include <QAxObject>
#include <QDebug>
#include <QStringList>

//MyTestModel::MyTestModel()
//    :iRow(400),iCol(6)
//{
//}

MyTestModel::MyTestModel(QString xls_file)
{
    openExcel(xls_file);
}

void MyTestModel::openExcel(QString fileName)
{
    QAxObject excel("Excel.Application");
    excel.setProperty("Visible", false);
    QAxObject *work_books = excel.querySubObject("WorkBooks");
    work_books->dynamicCall("Open(const QString&)", fileName);

    QAxObject *work_book = excel.querySubObject("ActiveWorkBook");
    QAxObject *work_sheets = work_book->querySubObject("Sheets");  //Sheets也可换用WorkSheets

    int sheet_count = work_sheets->property("Count").toInt();  //获取工作表数目
    qDebug() << "sheet_count = " << sheet_count;
    if (sheet_count > 0)
    {
        QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", 1);

        qDebug() << "文件数据读取中...";
        QVariant var = readAll(work_sheet);
        castVariant2ListListVariant(var);
    }

    work_book->dynamicCall("Close(Boolean)", false);  //关闭文件
    excel.dynamicCall("Quit(void)");  //退出
}

QVariant MyTestModel::readAll(QAxObject *sheet)
{
    QVariant var;
    if (sheet != NULL && !sheet->isNull())
    {
        QAxObject *usedRange = sheet->querySubObject("UsedRange");
        if (NULL == usedRange || usedRange->isNull())
        {
            return var;
        }
        var = usedRange->dynamicCall("Value");
        delete usedRange;
    }
    return var;
}

void MyTestModel::castVariant2ListListVariant(const QVariant &var)
{
    varRows = var.toList();
    if (varRows.isEmpty())
    {
        return;
    }

    const int rowCount = varRows.size();
    qDebug()<<"====MyTestModel========="<<rowCount;
    QVariantList rowData;
    for (int i = 0; i < rowCount; ++i)
    {
        rowData = varRows[i].toList();
        if (i == 0)
        {
            foreach (auto item , rowData)
            {
                QString value = item.toString();
                headers.append(value);
            }
            // ui->tableWidget->setHorizontalHeaderLabels(headers);

            iCol =  headers.size();
            iRow = varRows.size();
            break;
        }
    }


}

QVariant MyTestModel::data(const QModelIndex &index, int role) const
{


    QVariantList rowData = varRows[index.row()].toList();
    QString stre = rowData[index.column()].toString();


    switch(role)
    {
    case Qt::DisplayRole:
        return QVariant(QString(tr("%1")).arg(stre));
    case Qt::ForegroundRole:
        switch(index.column() % 3)
        {
        case 0:
            return QVariant(QColor(Qt::red));
        case 1:
            return QVariant(QColor(Qt::green));
        case 2:
            return QVariant(QColor(Qt::blue));
        default://only to disable warning
            return QVariant(QColor(Qt::white));
        }

        break;
    default:
        return QVariant();
    }
}


int MyTestModel::rowCount(const QModelIndex & /*parent*/) const
{
        return iRow;
}


int MyTestModel::columnCount(const QModelIndex & /*parent*/) const
{
        return iCol;
}

void MyTestModel::setMyData(int idata)
{
    iRow = idata;
    //emit dataChanged(createIndex(0,0), createIndex(iRow,iCol));
    //reset();
}

QVariant MyTestModel::headerData (int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
        return headers[section];
    return QAbstractTableModel::headerData (section, orientation, role);
}
